package com.frxuexi.sign.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.text.set
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.base.entity.SignBeanItem
import com.frxuexi.base.http.Const
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.UiState
import com.frxuexi.sign.R
import com.frxuexi.sign.SignAdapter
import com.frxuexi.sign.SignIntent
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.databinding.ActivitySignListBinding
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/sign/SignListActivity")
@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding,SignIntent,SignViewModel>() {

    val signAdapter by lazy { SignAdapter() }
    val list= mutableListOf<SignBeanItem>()

    var first=true//是否是开始时间

    override fun getVM(): SignViewModel {
        return viewModels<SignViewModel>().value
    }

    override fun getBind(): ActivitySignListBinding {
        return ActivitySignListBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        val items = it.data as Collection<SignBeanItem>
                        list.clear()
                        list.addAll(items.sortedByDescending { it.id })
                        signAdapter.apply {
                            data.clear()
                            addData(list)
                            notifyDataSetChanged()
                        }
                    }

                    else -> {}
                }
            }
        }

    }

    override fun initView() {
        binding.signStatusTab.apply {
            addTab(newTab().setText("全部状态"),true)
            addTab(newTab().setText("迟到"),false)
            addTab(newTab().setText("早退"),false)
            addTab(newTab().setText("缺勤"),false)
            getTabAt(0)?.view?.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_blue)
            getTabAt(1)?.view?.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_lightgray)
            getTabAt(2)?.view?.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_lightgray)
            getTabAt(3)?.view?.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_lightgray)
            addOnTabSelectedListener(object :OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab!!.view.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_blue)
                    signAdapter.apply {
                        data.clear()
                        if(tab.position==0){
                            addData(list)
                        }else{
                            addData(list.filter { it.status==tab.text })
                        }
                        notifyDataSetChanged()
                    }

                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab!!.view.setBackgroundResource(com.frxuexi.base.R.drawable.shape_solid_lightgray)
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }
            })
        }

        binding.signRv.apply {
            adapter=signAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity)
        }

        vm.sendIntent(SignIntent.getSigns(SPUtils.getInstance().getInt(Const.UID)))

        binding.signFilter.setOnClickListener {
            showDateDialog {date->

                /*//根据日期筛查
                binding.signDate.setText(date)
                signAdapter.apply {
                    data.clear()
                    addData(list.filter { it.morningCheckIn.split(" ")[0] ==date })
                    notifyDataSetChanged()
                }*/
                if(first){
                    binding.signDate.setText(date)
                }else{
                    binding.signDate.append("至${date}")
                    signAdapter.apply {
                        data.clear()
                        val split = binding.signDate.text.split("至")
                        addData(list.filter { it.morningCheckIn.split(" ")[0] in split[0] .. split[1] })
                        notifyDataSetChanged()
                    }
                }
                first=!first

            }
        }
    }

}